package sort;

import java.util.Random;

/**
 * @author RunningShrimp
 * @date 2021/6/9  10:35
 * @see <a href=""></a>
 */
@SuppressWarnings("unchecked")
public abstract class BaseSort<E extends Comparable<E>> {
    /**
     * 排序
     *
     * @param array
     */
    protected abstract void sort(E[] array);

    protected boolean less(E i, E j) {
        return i.compareTo(j) < 0;
    }

    protected void exchange(E[] array, int i, int j) {
        E e = array[i];
        array[i] = array[j];
        array[j] = e;
    }

    public void show(E[] array) {
        for (E e : array) {
            System.out.print(e + "  ");
        }
        System.out.println();
    }

    public boolean isSorted(E[] array) {
        for (int i = 1; i < array.length; i++) {
            if (less(array[i - 1], array[i])) {
                return false;
            }
        }
        return true;
    }

    public static Integer[] generateArray(int n, int max) {
        Integer[] array = new Integer[n];
        Random random = new Random(System.currentTimeMillis());
        for (int i = 0; i < n; i++) {
            array[i] = random.nextInt(max);
        }
        return array;
    }

    public void time(E[] array) {
        long start = System.currentTimeMillis();
        sort(array);
        long end = System.currentTimeMillis();
        System.out.println("所用时间：" + ((end - start) * 1.0 / 1000));
    }
}
